<template>
	<view class="wrapper">
		<!-- 收货信息 -->
		<view v-if="address" class="shipment">
			<view class="dt">收货人: </view>
			<view class="dd meta">
				<text class="name">{{ address.userName }}</text>
				<text class="phone">{{ address.telNumber }}</text>
			</view>
			<view class="dt">收货地址:</view>
			<view class="dd">{{address.provinceName}}{{address.cityName}}{{address.countyName}}{{address.detailInfo}}
			</view>
		</view>
		<button v-else type="primary" style="margin: 15px;" @click="addAddress">添加收货地址</button>
		<!-- 购物车 -->
		<view class="carts">
			<view class="item">
				<!-- 店铺名称 -->
				<view class="shopname">优购生活馆</view>
				<view v-for="(item, index) in cartGoodsList" :key="item.goods_id" class="goods">
					<!-- 商品图片 -->
					<image class="pic" :src="item.goods_small_logo"></image>
					<!-- 商品信息 -->
					<view class="meta">
						<view class="name">{{ item.goods_name }}</view>
						<view class="price">
							<text>￥</text>{{ item.goods_price }}<text>.00</text>
						</view>
						<!-- 加减 -->
						<view class="amount">
							<text class="reduce" @click="changeNumber(index, -1)">-</text>
							<input type="number" :value="item.goods_number" class="number">
							<text class="plus" @click="changeNumber(index, 1)">+</text>
						</view>
					</view>
					<!-- 选框 -->
					<view class="checkbox" @click="changeState(index)">
						<icon type="success" size="20" :color="item.goods_state ? '#ea4451' : '#ccc'"></icon>
					</view>
				</view>
			</view>
		</view>
		<!-- 其它 -->
		<view class="extra">
			<label class="checkall" @click="changeAllState">
				<icon type="success" :color="isAllSelected ? '#ea4451' : '#ccc'" size="20"></icon>
				全选
			</label>
			<view class="total">
				合计: <text>￥</text><label>14110</label><text>.00</text>
			</view>
			<view class="pay" @click="goPay">结算({{totalNum}})</view>
		</view>
	</view>
</template>

<script>
	// this.$store.state.cart.cartGoodsList
	import {
		mapState,
		mapGetters
	} from 'vuex'

	export default {
		data() {
			return {
				order_number: ''
			}
		},
		computed: {
			...mapState('cart', ['cartGoodsList']),
			...mapGetters('cart', ['isAllSelected', 'totalNum']),
			...mapState('user', ['address'])
		},
		methods: {
			// 修改商品选中状态
			changeState(index) {
				// 调用mutation函数，修改选中状态
				this.$store.commit('cart/changeState', index)
			},
			// 点击全选
			changeAllState() {
				this.$store.commit('cart/changeAllState', !this.isAllSelected)
			},
			// 修改商品数量
			changeNumber(index, num) {
				// 调用mutation函数，修改商品数量
				this.$store.commit('cart/changeNumber', {
					index,
					num
				})
			},
			// 添加收货地址
			async addAddress() {
				const res = await uni.chooseAddress()
				// res：收货地址信息
				console.log(res);
				// 将收获地址信息存入全局数据
				this.$store.commit('user/addAddress', res)
				// 将收获地址信息存入本地数据
				uni.setStorageSync('address', res)
			},
			// 结算
			async goPay() {
				// 验证用户是否选择了商品
				const hasSelectGoods = this.cartGoodsList.some(item => item.goods_state)
				if (!hasSelectGoods) {
					uni.showToast({
						title: '请选择至少一件商品',
						icon: 'none'
					})
					return
				}
				// 验证用户是否选择了收货地址
				if (!this.address) {
					uni.showToast({
						title: '请选择收货地址',
						icon: 'none'
					})
					return
				}
				// 调用接口，生成订单
				const res = await uni.$http.post('/api/public/v1/my/orders/create', {
					order_price: 100,
					consignee_addr: this.address.provinceName + this.address.cityName + this.address
						.countyName + this.address.detailInfo,
					goods: this.cartGoodsList.filter(item => item.goods_state)
				})
				// console.log(res);
				// 记录生成的订单号
				this.order_number = res.data.message.order_number
				// 如果订单号不存在，提示创建订单失败
				if (!this.order_number) {
					uni.showToast({
						title: '创建订单失败',
						icon: 'error'
					})
				}
				// 生成预支付信息
				const result = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
					order_number: this.order_number
				})
				// result.data.message.pay：预支付信息
				console.log(result.data.message.pay);
				// 支付
				uni.requestPayment({
					...result.data.message.pay,
					complete: () => {
						// 跳转到订单列表页
						uni.navigateTo({
							url: '/subpkg/pages/order/index'
						})
					}
				})
			}
		},
	}
</script>

<style scoped lang="scss">
	.shipment {
		height: 100rpx;
		line-height: 2;
		padding: 30rpx 30rpx 40rpx 30rpx;
		font-size: 27rpx;
		color: #333;
		background-color: #fff;
		background-image: url(http://static.botue.com/ugo/images/cart_border%402x.png);
		background-size: contain;
		background-repeat: no-repeat;
		background-position: bottom;

		.dt {
			width: 140rpx;
			float: left;
			clear: both;
		}

		.dd {
			padding-left: 160rpx;
		}

		.meta {
			padding-right: 50rpx;
		}

		text.phone {
			float: right;
		}
	}

	.carts {
		background-color: #f4f4f4;
		padding-bottom: 110rpx;
		overflow: hidden;

		.shopname {
			padding: 30rpx;
			margin-top: 20rpx;
			font-size: 30rpx;
			color: #333;
			background-color: #fff;
			border-top: 1rpx solid #eee;
			border-bottom: 1rpx solid #eee;
		}

		.goods {
			display: flex;
			padding: 30rpx 20rpx 30rpx 0;
			margin-left: 105rpx;
			border-bottom: 1rpx solid #eee;
			background-color: #fff;

			position: relative;

			.checkbox {
				width: 101rpx;
				height: 100%;
				background-color: #fff;

				display: flex;
				justify-content: center;
				align-items: center;

				position: absolute;
				left: -100rpx;
				top: 0;
			}

			&:last-child {
				border-bottom: none;
			}

			.pic {
				width: 200rpx;
				height: 200rpx;
				margin-right: 30rpx;
			}

			.meta {
				flex: 1;
				font-size: 27rpx;
				color: #333;
				position: relative;
			}

			.name {
				width: 100%;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}

			.price {
				position: absolute;
				bottom: 0;

				color: #ea4451;
				font-size: 33rpx;

				text {
					font-size: 22rpx;
				}
			}

			.amount {
				position: absolute;
				bottom: 0;
				right: 20rpx;

				height: 48rpx;
				text-align: center;
				border: 1rpx solid #ddd;
				border-radius: 8rpx;

				display: flex;
				align-items: center;

				text {
					display: block;
					width: 60rpx;
					line-height: 48rpx;
					font-size: 36rpx;
					color: #ddd;
					text-align: center;
				}

				input {
					width: 60rpx;
					height: 48rpx;
					min-height: 48rpx;
					font-size: 27rpx;
					border-left: 1rpx solid #ddd;
					border-right: 1rpx solid #ddd;
				}
			}
		}
	}

	.extra {
		position: fixed;
		bottom: 0;
		/* #ifdef H5 */
		bottom: 50px;
		/* #endif */
		left: 0;
		z-index: 9;

		width: 750rpx;
		height: 96rpx;
		text-align: center;
		line-height: 96rpx;
		font-size: 36rpx;
		border-top: 1rpx solid #eee;
		background-color: #fff;
		color: #333;
		display: flex;

		.checkall {
			width: 140rpx;
			line-height: 1;
			margin-left: 25rpx;
			display: flex;
			align-items: center;

			icon {
				margin-right: 20rpx;
			}
		}

		.total {
			display: flex;
			justify-content: center;
			flex: 1;

			label,
			text {
				color: #ea4451;
				vertical-align: bottom;
				position: relative;
				bottom: -2rpx;
			}

			text {
				position: relative;
				bottom: -3rpx;
				font-size: 24rpx;

				&:first-child {
					margin-left: 10rpx;
				}
			}
		}

		.pay {
			width: 200rpx;
			background-color: #ea4451;
			color: #fff;
		}
	}
</style>
